<?php

class Admin_Model_Newscat extends Zend_Db_Table_Abstract
{
    protected $_name = 'newscat';        
    
    public function getAllData(){        
		$query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('c'=>$this->_name));                                
        $modelCat = new Admin_Model_Newscat;
        $cols=$modelCat->info(Zend_Db_Table_Abstract::COLS);        
        $catCols=array();
        foreach($cols as $item){            
            $catCols[]=$item.' as parent_'.$item;            
        }                                       
        $query->joinLeft(array('P'=>'newscat'),'c.parent=p.id',$catCols);
        $query->order('ord asc');  
        return $this->fetchAll($query);                                                                                                             
    }
        
    public function getData($data){       
        $query = $this->select()
                        ->from($this->_name);
        if(isset($data['id']) && $data['id']){
            $query->where('id = ?',$data['id']);
        }
        if(isset($data['name']) && $data['name']){
            $query->where('name = ?',$data['name']);
        }
        if(isset($data['parent']) && $data['parent']){
            $query->where('parent = ?',$data['parent']);
        }                          
        return $this->fetchAll($query);
    }        
        
    public function getDataByCatId($data){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('c'=>$this->_name));
                        
        $modelCat = new Admin_Model_Newscat;
        $cols=$modelCat->info(Zend_Db_Table_Abstract::COLS);        
        $catCols=array();
        foreach($cols as $item){            
            $catCols[]=$item.' as parent_'.$item;            
        }                                       
        $query->joinLeft(array('P'=>'newscat'),'c.parent=p.id',$catCols);                
                        
        $query->where('c.id = ?',(int)$data);        
        return $this->fetchRow($query);
    }
    
    public function getFirstLevel(){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('c'=>$this->_name));                                
        $modelCat = new Admin_Model_Newscat;
        $cols=$modelCat->info(Zend_Db_Table_Abstract::COLS);        
        $catCols=array();
        foreach($cols as $item){            
            $catCols[]=$item.' as parent_'.$item;            
        }                                       
        $query->joinLeft(array('P'=>'newscat'),'c.parent=p.id',$catCols);
        $query->where('c.parent = 0');  
        return $this->fetchAll($query);                              
                                                                       
        if($kq=$this->fetchAll($se))
            return $kq->toArray();
        else
            return false; 
    }
    
    public function getDataByUrl($url){        
       $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('c'=>$this->_name));                                
        $modelCat = new Admin_Model_Newscat;
        $cols=$modelCat->info(Zend_Db_Table_Abstract::COLS);        
        $catCols=array();
        foreach($cols as $item){            
            $catCols[]=$item.' as parent_'.$item;            
        }                                       
        $query->joinLeft(array('P'=>'newscat'),'c.parent=p.id',$catCols);
        $query->where('c.seourl = "'.$url.'"');        
        return $this->fetchRow($query);                                                                               
    }
    
    public function addData($data=array()){                    
        $idAdd = $this->insert($data);                                
        return $idAdd;
    }
             
    public function updateData($id, $data=array()){
        return $this->update($data, 'id = '.(int)$id);
    }                       
    public function deleteData($catId){        
        $this->delete('id = '.(int)$catId);
    }               
}